import * as resourceGroupService from '../../services/resource/resourceGroup';
import {queryIpParamList} from '../../services/resource/ipParam'
import { querAssetListByParam } from '../../services/equipment';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryAssetType} from '../../services/assetType';
export default {
  namespace: 'resourceGroup',
  state: {
    assetTypeTree:[],
    assetBrandList:[],
    detailList:[],
    list: [],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',
    newKey : '',
    currentItem: {},
    viewModalVisible:false,
    ipParamIdList:[],
    assetItem:{},
    assetCurrent: 1,
    assetCurrentSize: 10,
    assetTotal: 0,
    assetList: [],
    viewItem:{},
    viewList:[],
    viewModalVisible: false,
  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },   
    queryIpParamListSuc(state, { payload: ipParamIdList }) {
      return { ...state, ipParamIdList };
    },

    queryListByParamSuc(state, { payload }) {
      return { ...state, ...payload };
    },

    changeDetailList(state, { payload }) {
      return { ...state, ...payload, secondModalVisible: false };
    },

    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {      
      return { ...state, assetBrandList };
    },
    showAddModal (state, { payload }) {
      return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    
    hideAddModal (state) {
      return { ...state, modalVisible: false, detailList:[], newKey : new Date().getTime() + '' }
    },
    showViewModal (state, { payload }) {
      return { ...state, ...payload, viewModalVisible: true }
    },   
    hideViewModal (state) {
      return { ...state, viewModalVisible: false, }
    },
    showSecondModal (state, { payload }) {
      return { ...state, ...payload, secondModalVisible: true }
    },
    hideSecondModal (state) {
      return { ...state,secondModalVisible: false }
    },
    showViewModal(state, { payload }) {
      return { ...state, ...payload, newKey: new Date().getTime() + '', viewModalVisible: true }
    },
    hideViewModal(state, { payload }) {
      return { ...state, ...payload, viewModalVisible: false, newKey: new Date().getTime() + '' }
    },
  },
  effects: {   
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(resourceGroupService.queryList,payload);
      yield put({type: 'queryIpParamList'});
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },

    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    }, 
    *qryById({ payload },{ call, put }){
      const { body } = yield call(resourceGroupService.qryById,payload);
      yield put({type: 'showAddModal', payload: { currentItem:body.data,detailList:body.data.resourceGroupAssets, modalType: 'edit',}});  
      yield put({type: 'queryAssetType'});
      yield put({ type: 'queryAssetBrand', payload: {  },});       
    },

    *onViewItem({ payload }, { call, put }) {
      const { body } = yield call(resourceGroupService.qryById, payload);
      yield put({
        type: 'showViewModal', payload: { viewItem: body.data, viewList:body.data.resourceGroupAssets },
      });
    },

    *queryAssetList({ payload }, { call, put }) {
      yield put({ type: 'querAssetListByParam', payload: {searchStatusType:4} });
      yield put({ type: 'showSecondModal', payload: { assetItem: payload} });
    },
    *queryIpParamList({ payload },{ call, put }){
      const { body } = yield call(queryIpParamList);
      yield put({ type: 'queryIpParamListSuc', payload: body.data });
    },
    *addModal({ payload },{ call, put }){
      yield put({type: 'showAddModal',payload: { modalType: 'create',}});
      yield put({type: 'queryAssetType'});
      yield put({ type: 'queryAssetBrand', payload: {  },});     
    },
    *querAssetListByParam({ payload },{ call, put }){
      const { body } = yield call(querAssetListByParam,payload);
      yield put({ type: 'queryListByParamSuc', payload: { assetList:body.data,assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});      
    },
    *create({ payload },{ call, put }){
      const { body } = yield call(resourceGroupService.create,payload.data);
      yield put({ type: 'reload'});
      yield put({type: 'hideAddModal'});
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(resourceGroupService.edit,payload.data);
      yield put({ type: 'queryList', payload:payload.search });
      yield put({type: 'hideAddModal'});
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(resourceGroupService.remove,payload.id);
      yield put({ type: 'queryList', payload:payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'networkName':'' }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/resourceGroup') {
          dispatch({ type: 'queryList', payload:{ 'groupName':'' }});
        }
      });
    },
  },
};
